package com.ruoyi.api.balance;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.RedisService;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.Supplier;
import com.ruoyi.system.domain.SupplyUser;
import com.ruoyi.system.service.IDeviceService;
import com.ruoyi.system.service.ISupplierService;
import com.ruoyi.system.service.ISupplyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BalanceBaseCtrl extends BaseController {

    @Autowired
    private RedisService redis;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ISupplyUserService supplyUserService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IDeviceService deviceService;

    //获取秤归属的商家信息
    public Device getRequstDevice(){
        String mac = request.getHeader("mac");
        Device device = new Device();
        device.setMac(mac);
        List<Device> devices = deviceService.selectDeviceList(device);
        if(devices.size()>0) {
            device = devices.get(0);
            return device;
        }else{
            return null;
        }
    }

    //清除TOKEN
    public void clearToken() {
        String key = Constants.SUPPLY_APP_TOKEN_KEY + request.getHeader("token");
        redis.delVal(key);
    }


    //存储登录用户信息
    public void setLoginSupplyUser(SupplyUser user,String token) {
        String key = Constants.SUPPLY_APP_TOKEN_KEY + token;
        Supplier supplier = supplierService.selectSupplierById(user.getSupplyId());
        JSONObject supplierJson = new JSONObject();
        supplierJson.put("supply_id",supplier.getId());
        supplierJson.put("supply_addr",supplier.getAddr());
        supplierJson.put("supply_name",supplier.getName());
        supplierJson.put("supply_phone",supplier.getPhone());
        supplierJson.put("supply_suppliName",supplier.getSuppliName());
        supplierJson.put("user_id",user.getId());
        supplierJson.put("user_name",user.getName());
        supplierJson.put("user_phone",user.getPhone());
        supplierJson.put("user_pwd",user.getPwd());
        supplierJson.put("user_ctime",user.getCtime());
        supplierJson.put("user_type",user.getType());
        supplierJson.put("user_supply_id",user.getSupplyId());
        supplierJson.put("user_head",user.getHead());
        supplierJson.put("user_status",user.getStatus());
        redis.setVal(key,supplierJson.toJSONString(),60*24*3);

    }

    //刷新用户信息
    public void refreshSupplyUser() {
        String key = Constants.SUPPLY_APP_TOKEN_KEY+request.getHeader("token");
        String supplierStr = redis.getStrVal(key);
        if(supplierStr!=null){
            JSONObject redisJson = JSON.parseObject(supplierStr);
            Long userId = redisJson.getLong("user_id");
            SupplyUser user = supplyUserService.selectSupplyUserById(userId);
            Supplier supplier = supplierService.selectSupplierById(user.getSupplyId());
            JSONObject supplierJson = new JSONObject();
            supplierJson.put("supply_id",supplier.getId());
            supplierJson.put("supply_addr",supplier.getAddr());
            supplierJson.put("supply_name",supplier.getName());
            supplierJson.put("supply_phone",supplier.getPhone());
            supplierJson.put("supply_suppliName",supplier.getSuppliName());
            supplierJson.put("user_id",user.getId());
            supplierJson.put("user_name",user.getName());
            supplierJson.put("user_phone",user.getPhone());
            supplierJson.put("user_pwd",user.getPwd());
            supplierJson.put("user_ctime",user.getCtime());
            supplierJson.put("user_type",user.getType());
            supplierJson.put("user_supply_id",user.getSupplyId());
            supplierJson.put("user_head",user.getHead());
            supplierJson.put("user_status",user.getStatus());
            redis.setVal(key,supplierJson.toJSONString(),30);

        }
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    public SupplyUser getLoginSupplyUser(){
        String key = Constants.SUPPLY_APP_TOKEN_KEY+request.getHeader("token");
        String supplierStr = redis.getStrVal(key);
        if(supplierStr!=null){
            JSONObject supplierJson = JSON.parseObject(supplierStr);
            SupplyUser user = new SupplyUser();
            user.setId(supplierJson.getLong("user_id"));
            user.setName(supplierJson.getString("user_name"));
            user.setPhone(supplierJson.getString("user_phone"));
            user.setPwd(supplierJson.getString("user_pwd"));
            user.setCtime(supplierJson.getDate("user_ctime"));
            user.setType(supplierJson.getString("user_type"));
            user.setSupplyId(supplierJson.getLong("user_supply_id"));
            user.setHead(supplierJson.getString("user_head"));
            user.setStatus(supplierJson.getString("user_status"));
            return user;
        }
        //如果没有，那就是测试号,并且没有放入redis
        else{
            SupplyUser user = supplyUserService.selectSupplyUserById(1L);//\测试号
            Supplier supplier = supplierService.selectSupplierById(user.getSupplyId());
            JSONObject supplierJson = new JSONObject();
            supplierJson.put("supply_id",supplier.getId());
            supplierJson.put("supply_addr",supplier.getAddr());
            supplierJson.put("supply_name",supplier.getName());
            supplierJson.put("supply_phone",supplier.getPhone());
            supplierJson.put("supply_suppliName",supplier.getSuppliName());
            supplierJson.put("user_id",user.getId());
            supplierJson.put("user_name",user.getName());
            supplierJson.put("user_phone",user.getPhone());
            supplierJson.put("user_pwd",user.getPwd());
            supplierJson.put("user_ctime",user.getCtime());
            supplierJson.put("user_type",user.getType());
            supplierJson.put("user_supply_id",user.getSupplyId());
            supplierJson.put("user_head",user.getHead());
            supplierJson.put("user_status",user.getStatus());
            redis.setVal(key,supplierJson.toJSONString(),30);
            return user;
        }
    }

    /**
     * 获取当前登录用户所属的供应商信息
     * @return
     */
    public Supplier getLoginSupplier() {
        String key = Constants.SUPPLY_APP_TOKEN_KEY+request.getHeader("token");
        String supplierStr = redis.getStrVal(key);
        if(supplierStr!=null){
            JSONObject supplierJson = JSON.parseObject(supplierStr);
            Supplier supplier = new Supplier();
            supplier.setId(supplierJson.getLong("supply_id"));
            supplier.setAddr(supplierJson.getString("supply_addr"));
            supplier.setName(supplierJson.getString("supply_name"));
            supplier.setPhone(supplierJson.getString("supply_phone"));
            supplier.setSuppliName(supplierJson.getString("supply_suppliName"));
            return supplier;
        }
        //如果没有，那就是测试号,并且没有放入redis
        else{
            SupplyUser user = supplyUserService.selectSupplyUserById(1L);//\测试号
            Supplier supplier = supplierService.selectSupplierById(user.getSupplyId());
            JSONObject supplierJson = new JSONObject();
            supplierJson.put("supply_id",supplier.getId());
            supplierJson.put("supply_addr",supplier.getAddr());
            supplierJson.put("supply_name",supplier.getName());
            supplierJson.put("supply_phone",supplier.getPhone());
            supplierJson.put("supply_suppliName",supplier.getSuppliName());

            supplierJson.put("user_id",user.getId());
            supplierJson.put("user_name",user.getName());
            supplierJson.put("user_phone",user.getPhone());
            supplierJson.put("user_pwd",user.getPwd());
            supplierJson.put("user_ctime",user.getCtime());
            supplierJson.put("user_type",user.getType());
            supplierJson.put("user_supply_id",user.getSupplyId());
            supplierJson.put("user_head",user.getHead());
            supplierJson.put("user_status",user.getStatus());
            redis.setVal(key,supplierJson.toJSONString(),30);
            return supplier;
        }
    }

}
